<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" :inline="true">
      <el-form-item label="缓存数据类型" prop="cacheKey">
        <el-select v-model="queryParams.cacheKey" clearable filterable placeholder="请选择缓存数据类型" @change="changKey">
          <el-option v-for="item in keyList"
                     :key="item.code" :label="item.msg" :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="jvm缓存中版本号" prop="funcCode">
        <el-input
          v-model="queryParams.jvmVersion" :disabled="true"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="redis缓存中版本号" prop="funcCode">
        <el-input
          v-model="queryParams.redisVersion" :disabled="true"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:role:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:role:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:role:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getKeyList"></right-toolbar>
    </el-row>

    <div class="data">

      <div class="cacheData">
        <span >缓存中数据</span>
        <el-input
          type="textarea"
          :disabled="true"
          v-model="cacheData"
          :rows="30"
          show-word-limit
        >
        </el-input>
      </div>

      <div class="dbData">
        <span >数据库中数据</span>
        <el-input
          type="textarea"
          :disabled="true"
          v-model="dbData"
          :rows="30"
          show-word-limit
        >
        </el-input>
      </div>
    </div>


  </div>
</template>

<script>
  import {
    getKeyList,
    getVersion,
    getCacheData,
    getDatabaseData,
  } from "@/api/monitor/memory";


  export default {
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 表格数据
        page: null,
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        dialogType: '',
        // 查询参数
        queryParams: {
          cacheKey: undefined,
          jvmVersion: undefined,
          redisVersion: undefined,
        },
        keyList: [],
        cacheData: "",
        dbData: ""
      };
    },
    created() {
      this.getKeyList();
    },
    methods: {
      /** 查询分页列表 */
      getKeyList() {
        this.loading = true;
        getKeyList(this.queryParams).then(response => {
            this.keyList = response.data;
          }
        );
      },
      changKey(key) {
        getVersion(key).then(response => {
            let version = response.msg;
            let versions = version.split("_");
            this.queryParams.redisVersion = versions[0];
            this.queryParams.jvmVersion = versions[1];
          }
        );
        getCacheData(key).then(response => {
            this.cacheData = response.msg;
          }
        );
        getDatabaseData(key).then(response => {
            this.dbData = response.msg;
          }
        );

      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          identifierMsg: undefined,
          identifierValue: undefined,
          codingFormat: '',
          integerBit: undefined,
          radixPointBit: undefined,
          tableName: undefined,
          fieldName: undefined,
          fieldUnit: undefined,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;

      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.title = "新增测站与解析组关联关系";
        this.open = true;
        this.dialogType = "add";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids;
        getStGroup(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改测站与解析组关联关系";
          this.dialogType = "update";
        });
      },
      /** 查询所有接入测站列表 */
      getAllStation() {
        getAllStation().then(response => {
          this.allStationList = response.data;
        });
      },
      /** 查询所有接入解析组列表 */
      getAllElementGroup() {
        getAllElementGroup().then(response => {
          this.allElementGroupList = response.data;
        });
      },

      /** 提交按钮 */
      submitForm: function () {
        let that = this;
        that.$refs["form"].validate(valid => {
          if (valid) {
            if ('update' === that.dialogType) {
              updateStGroup(that.form).then(response => {
                that.$modal.msgSuccess("修改成功");
                that.open = false;
                that.getPage();
              });
            } else {
              insertStGroup(that.form).then(response => {
                that.$modal.msgSuccess("新增成功");
                that.open = false;
                that.getPage();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const id = row.id || this.ids;
        this.$modal.confirm('是否确认删除？').then(function () {
          return delStGroup(id);
        }).then(() => {
          this.getPage();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/user/export', {
          ...this.queryParams
        }, `user_${new Date().getTime()}.xlsx`)
      },
    }
  };
</script>

<style scoped lang="scss">

  .data {
    width: 100%;
    display: flex;
    height: 700px;
  }
  .data span {
    margin: 10px;
    display: block;
  }

  .cacheData {
    border: 1px #ccc solid;
    width: 49%;
    margin-right: 10px;
    border-radius: 4px;
    height: 100%;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
  }

  .dbData {
    width: 49%;
    border: 1px #ccc solid;
    margin-left: 10px;
    height: 100%;
    border-radius: 4px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1)
  }
</style>
